
%% @author Hee Yeon Cho <aposto@nexon.co.kr>
%% @copyright 2009 Nexon Corporation.

%% @doc 
%%      
	
-module(savage_util).
-author('aposto@nexon.co.kr').

-include_lib("eunit/include/eunit.hrl").

%% API
-export([bin_to_md5/1, make_hash/1 ]).

-export([init_seed/0]).

-export([millisec_time/0]).

-define(RANGE_MAX, 16#7ffffff).

%% test
%%  eunit:test(".", [verbose]).

%% External functions

%% @spec test() -> integer() | ok | undefined | error
%% @doc Run all of the tests.

bin_to_md5(Bin) ->
	C1 = erlang:md5_init(),
	C2 = erlang:md5_update(C1, Bin),
	erlang:md5_final(C2).
	
make_hash(UserName) ->
	erlang:phash2(UserName, ?RANGE_MAX).
	%erlang:phash2(UserName, (1 bsl 27)).


init_seed() ->
	{H,M,S}=time(),
	random:seed(H*13,M*7,S*3).
	
millisec_time() ->
	{A,B,C} = erlang:now(),
	A*1000000000+B*1000+(C div 1000).
		
%% Internal functions

%% Test suites
bin_to_md5_test() ->
	[ ?assertEqual( bin_to_md5(<<"kor-kr">>), bin_to_md5(<<"kor-kr">>) )
	].
	
make_hash_test() ->
	[ ?assert( make_hash("aaa") =/= make_hash("aab") ),
	?assertEqual( make_hash("56983"),  make_hash("56983") )
	].
	
 